<template>
  <!-- 引入公共布局，首页内容放在插槽里 -->
  <Layout>
    <!-- 首页加载状态 -->
    <div v-if="isLoading" class="loading-overlay">
      <div class="spinner"></div>
      <p class="loading-text">正在准备你的音乐创作空间...</p>
    </div>

    <!-- 首页主内容（只保留首页独有的部分） -->
    <template v-else>
      <!-- 欢迎区域 -->
      <section class="welcome-section">
        <h1>{{ welcomeText }}，{{ userName }} 👋</h1>
        <p class="welcome-desc">
          {{ userInfo.worksCount ? `你的${userInfo.worksCount}首作品正在等待灵感延续` : '今天也让AI帮你创造一段动人旋律吧' }}
        </p>
      </section>

      <!-- 功能卡片区域 -->
      <section class="features-grid">
        <div class="feature-card" @click="navigateTo('/compose')" :style="{ animationDelay: '0.1s' }">
          <div class="card-icon compose-icon">
            <i class="fa fa-music"></i>
          </div>
          <h3>AI作曲</h3>
          <p>输入风格（流行/古典/电子）和情绪，AI生成专属旋律</p>
          <button class="card-btn">开始创作</button>
        </div>

        <div class="feature-card" @click="navigateTo('/lyrics')" :style="{ animationDelay: '0.2s' }">
          <div class="card-icon lyrics-icon">
            <i class="fa fa-pencil"></i>
          </div>
          <h3>歌词生成</h3>
          <p>基于主题（爱情/梦想）自动生成押韵歌词，支持修改微调</p>
          <button class="card-btn">生成歌词</button>
        </div>

        <div class="feature-card" @click="navigateTo('/arrange')" :style="{ animationDelay: '0.3s' }">
          <div class="card-icon arrange-icon">
            <i class="fa fa-sliders"></i>
          </div>
          <h3>音乐改编</h3>
          <p>上传本地音频或作品，一键改编为不同风格版本</p>
          <button class="card-btn">开始改编</button>
        </div>

        <div class="feature-card" @click="navigateTo('/works')" :style="{ animationDelay: '0.4s' }">
          <div class="card-icon manage-icon">
            <i class="fa fa-folder-open"></i>
          </div>
          <h3>作品管理</h3>
          <p>查看、编辑、下载你的所有AI创作作品，支持分享</p>
          <span v-if="userInfo.worksCount" class="works-badge">{{ userInfo.worksCount }}</span>
          <button class="card-btn">查看作品</button>
        </div>
      </section>
    </template>
  </Layout>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import Layout from './Layout.vue'; // 引入公共布局组件

const router = useRouter();

// 首页独有的状态（公共状态在Layout里）
const isLoading = ref(true);
const userInfo = ref({
  worksCount: Number(localStorage.getItem('userWorksCount')) || 0,
  name: localStorage.getItem('currentUser') ? JSON.parse(localStorage.getItem('currentUser')).name : ''
});

// 计算属性：欢迎语和用户名
const welcomeText = computed(() => {
  const hour = new Date().getHours();
  if (hour >= 6 && hour < 12) return '早上好';
  if (hour >= 12 && hour < 18) return '下午好';
  if (hour >= 18 && hour < 22) return '晚上好';
  return '夜深了';
});
const userName = computed(() => {
  return userInfo.value.name || '亲爱的音乐创作者';
});

// 页面加载初始化
onMounted(async () => {
  try {
    // 验证登录状态（实际项目可和Layout的登录逻辑统一，用Pinia管理）
    const token = localStorage.getItem('userToken');
    if (!token) {
      setTimeout(() => {
        router.push({ path: '/login', query: { expired: '请先登录后使用' } });
      }, 800);
    }
  } catch (error) {
    console.error('首页初始化失败：', error);
  } finally {
    isLoading.value = false;
  }
});

// 导航跳转（和Layout保持一致）
const navigateTo = (path) => {
  router.push(path).catch(err => {
    console.error('导航失败：', err);
    alert('页面跳转失败，请刷新页面重试');
  });
};
</script>

<style scoped>
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}
.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid rgba(59, 130, 246, 0.3);
  border-top: 5px solid var(--primary-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 1rem;
}
.loading-text {
  color: var(--text-secondary);
  font-size: 1rem;
  opacity: 0;
  animation: fadeIn 0.5s ease 0.3s forwards;
}

.welcome-section {
  text-align: center;
  margin: 2rem 0 3rem;
  animation: fadeIn 0.8s ease-out;
  padding: 0 1rem;
}
.welcome-section h1 {
  font-size: 2.2rem;
  margin-bottom: 0.8rem;
  background: linear-gradient(90deg, var(--primary-color), var(--primary-hover));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 600;
}
.welcome-desc {
  font-size: 1.1rem;
  color: var(--text-tertiary);
  max-width: 700px;
  margin: 0 auto;
  line-height: 1.5;
  opacity: 0.9;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 2rem;
  margin-top: 1rem;
}
.feature-card {
  background: var(--bg-secondary);
  border-radius: 10px;
  padding: 1.5rem;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid transparent;
  cursor: pointer;
  opacity: 0;
  transform: translateY(20px);
  animation: cardFadeIn 0.5s ease forwards;
  position: relative;
  overflow: hidden;
}
.feature-card:hover {
  transform: translateY(-8px);
  border-color: var(--primary-color);
  box-shadow: 0 15px 30px -5px rgba(59, 130, 246, 0.15);
  background: var(--bg-hover);
}

.works-badge {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: var(--primary-color);
  color: var(--text-primary);
  font-size: 0.8rem;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

.card-icon {
  width: 60px;
  height: 60px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 1rem;
  font-size: 1.8rem;
  transition: transform 0.3s ease;
}
.feature-card:hover .card-icon {
  transform: scale(1.1);
}

.compose-icon {
  background: rgba(59, 130, 246, 0.2);
  color: var(--primary-color);
}
.lyrics-icon {
  background: rgba(16, 185, 129, 0.2);
  color: #10b981;
}
.arrange-icon {
  background: rgba(245, 158, 11, 0.2);
  color: #f59e0b;
}
.manage-icon {
  background: rgba(236, 72, 153, 0.2);
  color: #ec4899;
}

.feature-card h3 {
  font-size: 1.3rem;
  margin-bottom: 0.5rem;
  color: var(--text-primary);
  font-weight: 500;
}
.feature-card p {
  color: var(--text-tertiary);
  margin-bottom: 1.5rem;
  font-size: 0.95rem;
  line-height: 1.4;
}

.card-btn {
  background: var(--bg-secondary);
  border: 1px solid var(--primary-color);
  color: var(--primary-color);
  padding: 0.6rem 1.2rem;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  width: 100%;
  font-weight: 500;
  font-size: 0.95rem;
  outline: none;
}
.card-btn:hover {
  background: var(--primary-color);
  color: var(--text-primary);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}
.card-btn:focus {
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.3);
}

/* 动画 */
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes cardFadeIn {
  to { opacity: 1; transform: translateY(0); }
}

/* 响应式 */
@media (max-width: 1024px) {
  .features-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
@media (max-width: 480px) {
  .features-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }
  .welcome-section h1 {
    font-size: 1.5rem;
  }
}
</style>